﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spotless.Data;
using Spotless.Data.DataAccess.Interfaces;

namespace Spotless.Services
{
    public class CampaignService
    {
        public CampaignService()
            : this(null)
        {
        }

        public CampaignService(IQueryRepository queryRepository)
        {
            this.QueryRepository = queryRepository ?? new Spotless.Data.DataAccess.Sql.QueryRepository();
        }

        protected virtual IQueryRepository QueryRepository { get; private set; }

        public List<Customer> GetAllCustomers()
        {
            return QueryRepository.Customer_GetAll().ToList();
        }

        public Campaign CreateCampaign()
        {
            var campaign = new Campaign();
            QueryRepository.AddCampaign(campaign);
            return campaign;
        }
        //

        //public List<Campaign> GetAllCampaigns()
        //{
            
        //    return QueryRepository.Campaign_GetAll().ToList();
        //}
        //
        
        public PagedList<Campaign> GetAllCampaigns(string sort, int page, int pageSize)
        {
            var tmp = QueryRepository.Campaign_GetAll();
            if (sort != null)
            {
                var sortExpression = SortExpression<Campaign>.Parse(sort);
                tmp = sortExpression.Sort(tmp);
            }
            return tmp.ToPagedList(page, pageSize);
        }
        //
        
        public Campaign CampaignGetById(int id)
        {
            return QueryRepository.Campaign_GetById(id);
            //  Spotless.Data.Query Query_GetById(int id);
        }

        public List<CampaignUser> CampaignGetRecipients(int id)
        {
            //return QueryRepository.Campaign_Recipients(id);
            return QueryRepository.CampaignUser_Recipients(id).ToList();

        }

        public List<Campaign> CampaignGetByIdList(int id)
        {
            return QueryRepository.Campaign_GetByIdList(id).ToList();
        }

        //GET CATEGORIES FOR DROP DOWN
        public List<Category> GetCategories()
        {
            return QueryRepository.Category_GetAll().ToList();
        }

        public void SaveChanges()
        {
            QueryRepository.SaveChanges();
        }

        public Category GetCategoryById(int categoryId)
        {
            return QueryRepository.Category_GetById(categoryId);
        }

        public List<DrillDown> GetTopLevelDrillDown(int categoryId, byte typeId)
        {
            return QueryRepository.DrillDown_GetTopLevelByCategoryId(categoryId, typeId).ToList();
        }

        public List<DrillDown> GetTopLevelDrillDown(int categoryId, DrillDown.DrillDownTypes type)
        {
            return QueryRepository.DrillDown_GetTopLevelByCategoryId(categoryId, (byte)type).ToList();
        }

        public List<DrillDown> GetDrillDownsByParentId(int id)
        {
            return QueryRepository.DrillDown_GetAllByParentId(id).ToList();
        }

        public List<DrillDown> GetDrillDownsByParentId(int id, DrillDown.DrillDownTypes type)
        {
            return QueryRepository.DrillDown_GetAllByParentId(id, (byte)type).ToList();
        }

        public DrillDown GetDrillDownById(int id)
        {
            return QueryRepository.DrillDown_GetById(id);
        }

        public void AddUsers(Campaign campagin)
        {
            if (campagin.ProductId == null)
                throw new ArgumentNullException("Campign Dosnt have a product Id");
            var userIds = QueryRepository.Customer_GetAll().Where(x => x.ProductId == campagin.ProductId).Select(x => x.CustomerId).ToArray();
            campagin.CampaignUsers.AddRange(userIds.Select(x => new CampaignUser() { Campaign = campagin, CustomerId = x }));
        }


        public List<CampaignUser> CampaignUserGetAll(){
            return QueryRepository.CampaignUser_GetAll().ToList(); ;
        }

 
        public List<CampaignUser> GetCampaignRecipents(int id) {
            return QueryRepository.CampaignUser_Recipients(id).ToList();
        }


        ///
        //start
        public IQueryable<CampaignUser> GetAllCampaignRecipents(int id, Dictionary<FilterExpression<CampaignUser>, string> filters)
        {


            var tmp = QueryRepository.CampaignUser_Recipients(id);
            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    tmp = filter.Key.Filter(tmp, filter.Value);
                }
            }
            return tmp;



        }

        public PagedList<CampaignUser> GetAllCampaignRecipents(int id, Dictionary<FilterExpression<CampaignUser>, string> filters, string sort, int page, int pageSize)
        {
            var tmp = GetAllCampaignRecipents(id, filters);
            if (sort != null)
            {
                var sortExpression = SortExpression<CampaignUser>.Parse(sort);
                tmp = sortExpression.Sort(tmp);
            }
            return tmp.ToPagedList(page, pageSize);
            //
        }
        

        //
        

        
    }
}
